/*     */ package com.zimbra.cs.service.admin;
/*     */ 
/*     */ import com.google.common.collect.Sets;
/*     */ import com.zimbra.common.account.Key.ServerBy;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.SoapFaultException;
/*     */ import com.zimbra.common.util.CsvReader;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.accesscontrol.AdminRight;
/*     */ import com.zimbra.cs.account.accesscontrol.Rights.Admin;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import com.zimbra.soap.admin.message.GetServiceStatusResponse;
/*     */ import com.zimbra.soap.admin.type.ServiceStatus;
/*     */ import com.zimbra.soap.admin.type.TimeZoneInfo;
/*     */ import com.zimbra.soap.type.ZeroOrOne;
/*     */ import java.io.BufferedReader;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStreamReader;
/*     */ import java.io.StringReader;
/*     */ import java.io.StringWriter;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.TimeZone;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class GetServiceStatus
/*     */   extends AdminDocumentHandler
/*     */ {
/*  56 */   private static final String ZMRRDFETCH = LC.zimbra_home.value() + "/libexec/zmrrdfetch";
/*     */   private static final String ZMSTATUSLOG_CSV = "zmstatuslog";
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context) throws SoapFaultException, ServiceException
/*     */   {
/*  61 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*     */     
/*     */ 
/*  64 */     Provisioning prov = Provisioning.getInstance();
/*  65 */     String monitorHost = prov.getConfig().getAttr("zimbraLogHostname");
/*  66 */     if ((monitorHost == null) || (monitorHost.trim().equals("")))
/*  67 */       throw ServiceException.FAILURE("zimbraLogHostname is not configured", null);
/*  68 */     Server monitorServer = prov.get(Key.ServerBy.name, monitorHost);
/*  69 */     if (monitorServer == null) {
/*  70 */       throw ServiceException.FAILURE("could not find zimbraLogHostname server: " + monitorServer, null);
/*     */     }
/*  72 */     if (!prov.getLocalServer().getId().equalsIgnoreCase(monitorServer.getId())) {
/*  73 */       return proxyRequest(request, context, monitorServer);
/*     */     }
/*  75 */     GetServiceStatusResponse resp = new GetServiceStatusResponse();
/*     */     
/*  77 */     TimeZone tz = TimeZone.getDefault();
/*  78 */     TimeZoneInfo timezone = TimeZoneInfo.fromIdAndDisplayName(tz.getID(), tz.getDisplayName());
/*     */     
/*  80 */     resp.setTimezone(timezone);
/*     */     
/*  82 */     boolean loggerEnabled = false;
/*  83 */     Server local = prov.getLocalServer();
/*  84 */     String[] services = local.getMultiAttr("zimbraServiceEnabled");
/*  85 */     if (services != null) {
/*  86 */       for (int i = 0; (i < services.length) && (!loggerEnabled); i++) {
/*  87 */         loggerEnabled = "logger".equals(services[i]);
/*     */       }
/*     */     }
/*  90 */     if (loggerEnabled) {
/*  91 */       HashSet<ServiceStatus> serviceStatus = Sets.newHashSet();
/*  92 */       List<Server> servers = prov.getAllServers();
/*  93 */       for (Server s : servers) {
/*  94 */         String[] srvs = s.getMultiAttr("zimbraServiceEnabled");
/*  95 */         for (String service : srvs) {
/*  96 */           serviceStatus.add(ServiceStatus.fromServerServiceTimeStatus(s.getName(), service, System.currentTimeMillis() / 1000L, ZeroOrOne.ZERO));
/*     */         }
/*     */       }
/*     */       
/*     */ 
/*     */ 
/*     */ 
/* 103 */       BufferedReader in = null;
/*     */       try {
/* 105 */         ProcessBuilder pb = new ProcessBuilder(new String[] { ZMRRDFETCH, "-f", "zmstatuslog" });
/* 106 */         Process p = pb.start();
/* 107 */         in = new BufferedReader(new InputStreamReader(p.getInputStream()));
/* 108 */         Map<String, CsvReader> hostStatus = new HashMap();
/* 109 */         StringWriter currentWriter = null;
/* 110 */         String currentHost = null;
/*     */         String line;
/* 112 */         while ((line = in.readLine()) != null) {
/* 113 */           if (!"".equals(line.trim()))
/* 114 */             if (line.startsWith("Host: ")) {
/* 115 */               if (currentHost != null) {
/* 116 */                 hostStatus.put(currentHost, new CsvReader(new StringReader(currentWriter.toString())));
/*     */               }
/* 118 */               currentHost = line.substring("Host: ".length());
/* 119 */               currentWriter = new StringWriter();
/*     */             }
/* 121 */             else if (currentWriter != null) {
/* 122 */               currentWriter.write(line + "\n");
/*     */             }
/*     */         }
/* 125 */         if ((currentHost != null) && (currentWriter != null)) {
/* 126 */           hostStatus.put(currentHost, new CsvReader(new StringReader(currentWriter.toString())));
/*     */         }
/* 128 */         List<ServiceStatus> status = ServiceStatus.parseData(hostStatus);
/* 129 */         for (ServiceStatus stat : status) {
/* 130 */           serviceStatus.remove(stat);
/* 131 */           serviceStatus.add(stat);
/*     */         }
/* 133 */         for (ServiceStatus stat : serviceStatus) {
/* 134 */           if (!checkRights(zsc, stat.getServer())) {
/* 135 */             ZimbraLog.misc.info("skipping server " + stat.getServer() + ", has not right to get service status");
/*     */           }
/*     */           else
/*     */           {
/* 139 */             resp.addServiceStatus(stat);
/*     */           }
/*     */         }
/*     */       } catch (IOException e) {
/*     */         try {
/* 144 */           if (in != null) in.close();
/*     */         }
/*     */         catch (IOException x) {}
/* 147 */         ServiceException.FAILURE("Unable to read logger stats", e);
/*     */       }
/*     */     }
/* 150 */     return zsc.jaxbToElement(resp);
/*     */   }
/*     */   
/*     */   private boolean checkRights(ZimbraSoapContext zsc, String serverName) throws ServiceException {
/*     */     try {
/* 155 */       Server server = Provisioning.getInstance().get(Key.ServerBy.name, serverName);
/* 156 */       checkRight(zsc, server, Rights.Admin.R_getServiceStatus);
/*     */     }
/*     */     catch (ServiceException e)
/*     */     {
/* 160 */       if ("service.PERM_DENIED".equals(e.getCode())) {
/* 161 */         return false;
/*     */       }
/*     */       
/* 164 */       throw e;
/*     */     }
/*     */     
/*     */ 
/* 168 */     return true;
/*     */   }
/*     */   
/*     */   public void docRights(List<AdminRight> relatedRights, List<String> notes)
/*     */   {
/* 173 */     relatedRights.add(Rights.Admin.R_getServiceStatus);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/admin/GetServiceStatus.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */